

library(ggplot2)
library(dplyr)

# 数据准备
data <- data.frame(
  category = c("A", "B", "C", "D"),
  value = c(25, 30, 15, 30)
)

# 计算标签位置
data <- data %>%
  mutate(
    percentage = value / sum(value) * 100,
    ymax = cumsum(value),
    ymin = c(0, head(ymax, n=-1)),
    label_position = (ymax + ymin) / 2,
    label = sprintf("%.1f%%", percentage)
  )

# 绘图
ggplot(data, aes(ymax = ymax, ymin = ymin, xmax = 4, xmin = 3, fill = category)) +
  geom_rect(show.legend = TRUE) +
  coord_polar(theta = "y") +
  xlim(c(0, 5)) +
  theme_void() +
  geom_text(
    aes(x = 4.5, y = label_position, label = label),
    color = "black", size = 4, inherit.aes = FALSE
  ) +
  scale_fill_manual(values = c("#FF6B6B", "#4ECDC4", "#45B7D1", "#96CEB4"))
